
import sys
import time
import random
import re
import pyautogui

from Config import *
from SpiderUtil import *
from ProxyUtil import *
from RabbitUtil import RabbitUtil
from SeleniumUtil import SeleniumUtil
from SeleniumUtil import CheckDriverExit
from TimeUtil    import *

#selenium
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
#反爬
import undetected_chromedriver as uc
#BeautifulSoup
from bs4 import BeautifulSoup

from pymysql.converters import escape_string

from ErrorCode import *

#多线程处理
from threading import Thread
#线程池相关
from concurrent.futures import ThreadPoolExecutor, wait, as_completed

# slideCount = 0

# 滑块go
def ScrollBarHandle(driver):
    # if slideCount > 10:
    #     slideCount = 0
    #     driver.refresh()

    slider = driver.find_element(By.CSS_SELECTOR, 'span#nc_1_n1z')
    # hkLocal = huakuaiEle.location
    # print (hkLocal)

    # 获取滑块的相对坐标和尺寸
    slider_location = slider.location
    slider_size = slider.size

    # 获取滑块相对于视口的位置
    slider_x = slider_location['x']
    slider_y = slider_location['y']

    # 使用JavaScript获取浏览器窗口的外部尺寸
    window_position = driver.execute_script("return {x: window.screenLeft, y: window.screenTop};")
    
    # 获取页面滚动的位置
    scroll_position = driver.execute_script("return {x: window.pageXOffset, y: window.pageYOffset};")

    # 打印调试信息
    print(f"窗口位置: {window_position}")
    print(f"页面滚动位置: {scroll_position}")

    # 滑块相对于屏幕的绝对位置
    slider_screen_x = window_position['x'] + slider_x - scroll_position['x']
    slider_screen_y = window_position['y'] + slider_y - scroll_position['y']

    print(f"滑块相对于屏幕的坐标: (x: {slider_screen_x}, y: {slider_screen_y})")


     # 将鼠标移动到坐标 (x, y)
    # pyautogui.moveTo(hkLocal.get('x'), hkLocal.get('y'))
    pyautogui.moveTo(800, 680)

    time.sleep(2)
    # pyautogui.mouseDown()
    pyautogui.mouseDown(button='left')


    pyautogui.dragRel(random.choice(range(320,330)), 0, duration=random.choice(range(3,10))) 
    pyautogui.mouseDown(button='left')
    
    # pyautogui.dragRel(20, 0, duration=1) 
    # pyautogui.dragRel(20, 0, duration=1) 

    # pyautogui.mouseUp(button='left')
    time.sleep(3)

    pyautogui.mouseUp()
    # slideCount = slideCount + 1

    soup = BeautifulSoup(driver.page_source, "lxml")
    sbBtn = soup.select('div.errloading')
    huakuai = soup.select('span#nc_1_n1z');
    
    # 出现了滑块
    if len(sbBtn) > 0 or len(huakuai) > 0:
        
        time.sleep(2)
        if len(sbBtn) > 0:
            driver.find_element(By.CSS_SELECTOR, 'div.errloading').click()
            time.sleep(2)
            ScrollBarHandle(driver)
        if len(huakuai) > 0:
            huakuaiEle = driver.find_element(By.CSS_SELECTOR, 'span#nc_1_n1z')
            if huakuaiEle.is_displayed():
                ScrollBarHandle(driver)


def LoginHandle(driver):
    ele = driver.find_element(By.CSS_SELECTOR, 'input#fm-login-id')
    ele.clear()
    ele.send_keys("jiang1111114")
    time.sleep(1)
    ele = driver.find_element(By.CSS_SELECTOR, 'input#fm-login-password')
    ele.clear()
    ele.send_keys("JiangXC123698")
    time.sleep(1)

    login_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//button[contains(@class, "fm-button") and contains(text(), "登录")]'))
    )
    login_button.click()
    time.sleep(2)
                
        
        
        









# 执行详情信息获取
if __name__ == "__main__":
    # 获取爬虫参数线程数
    
    SeleniumH = SeleniumUtil(ProxyInfo=None);
    driver = SeleniumH.GetDriver();

    time.sleep(3)
    driver.get("https://sf-item.taobao.com/sf_item/825074884423.htm");
    time.sleep(5)

    soup = BeautifulSoup(driver.page_source, "lxml");

    # 滑块
    huakuai = soup.select('span#nc_1_n1z');
    # 出现了滑块
    if len(huakuai) > 0:
        ScrollBarHandle(driver)
 
    # 出现在登录页面
    soup = BeautifulSoup(driver.page_source, "lxml")
    loginInput = soup.select('input#fm-login-id');
    if len(loginInput) > 0:
        LoginHandle(driver)
   


    sys.exit(0);


